Skip to content

Non-reverting encode/decode, AddressCoder dispatcher, EvmCoder - #1

Merged
adraffy merged 3 commits into
mainfrom
encode-bool-address-coder
Nov 28, 2026
Merged

adraffy merged 3 commits into
mainfrom
encode-bool-address-coder

Conversation

@adraffy

@adraffy adraffy commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Summary

Nothing reverts: encode and decode both return ok.

function encode(bytes memory data) internal pure returns (bool ok, string memory addr);
function decode(string memory addr) internal pure returns (bool ok, bytes memory data);

Invalid input gives (false, ""). A caller can't wrap an internal library call in try/catch, so with a reverting decoder a contract had no way to handle a bad address. Callers who want a revert can add their own (if (!ok) revert ...). All custom errors are gone. Coders with fixed-size input (Solana, Chia, Stellar, EVM) keep an infallible encode(bytes32) / encode(address) that returns a plain string.

The utils follow the same rule:

  • Base58, Base32, Bech32, CashAddr, SS58 and BitcoinLike decoders return ok.
  • Bech32.encode / encodeSegwit return false over the length limit or for an invalid witness program, which replaces encodedLength.
  • CashAddr.encode returns false for an unsupported hash size.
  • Blake2b.hash512 returns false without the precompile.

AddressCoder: an opt-in dispatcher by coinType.

(bool ok, string memory addr) = AddressCoder.encode(coinType, data);
(bool ok, bytes memory data) = AddressCoder.decode(coinType, addr);
bool supported = AddressCoder.isSupported(coinType);

It compiles to about 8–9 KB each for encode and decode. The per-chain coders don't import it, so tree-shaking is unchanged. For example, BitcoinCoder is about 3.3 KB for each direction.

EvmCoder covers ETH (COIN_TYPE_ETH = 60) and ENSIP-19 EVM chains (COIN_TYPE_DEFAULT | chainId, where COIN_TYPE_DEFAULT = 0x80000000). encode(data, checksum) / encode(address, checksum) emit EIP-55 or lowercase. With checksum, decode(addr, checksum) requires mixed-case input to be a valid EIP-55 checksum; all-lowercase and all-uppercase carry no checksum, so they're accepted. Without it, any case is accepted. The one-argument encode(bytes) / decode(string) use checksum = true, which matches the reference.

Blake2b fix. On a chain without the 0x09 precompile, the staticcall "succeeds" with empty return data, so Polkadot and Kusama silently produced a wrong checksum. It now checks returndatasize() == 64, and DOT/KSM return false on those chains.

References. Every coder and util now has a NatSpec References: list: the specs, where the chain's constants come from, and the matching address-encoder source file.

Tests

  • All vectors run through AddressCoder. The invalidAddress and invalidBytes cases must return ok == false with empty output, and must not revert.
  • testFuzz_encodeNeverReverts{,_shaped} run every supported coinType and some random ones. Whenever ok is true, the output must decode back to the input.
  • testFuzz_decodeNeverReverts{,_mutated} feed in arbitrary strings, and valid addresses with one character changed or the end cut off. Whenever ok is true, the decoded bytes must encode again.
  • test_missingPrecompile mocks 0x09 to return nothing; both DOT encode and DOT decode return false.
  • forge fmt --check, forge lint and forge test pass locally: 49 tests, 2000 fuzz runs each. I also ran the never-revert fuzzers once at 50k runs.

Breaking changes

  • XCoder.encode(bytes) returns (bool, string) and XCoder.decode(string) returns (bool, bytes).
  • Custom errors are removed, as are Blake2b.tryHash512 and Bech32.encodedLength.

🤖 Generated with Claude Code

adraffy and others added 2 commits November 27, 2026 15:16
- Every coder's encode now returns (bool ok, string addr) and never reverts:
  invalid bytes give (false, ""). decode still reverts on invalid input.
  Fixed-size coders keep an infallible encode(bytes32) / encode(address).
- AddressCoder: opt-in dispatcher by coinType (encode, decode, isSupported).
- EvmCoder: ETH (60) and ENSIP-11 EVM chains, EIP-55 checksummed.
- Blake2b: detect a missing 0x09 precompile (the call "succeeds" with no
  return data, which previously produced a wrong checksum silently);
  add tryHash512.
- Bech32: encodedLength / isValidWitnessProgram helpers for pre-checks.
- NatSpec References on every coder and util.
- Tests run through AddressCoder; fuzz that encode never reverts for any
  coinType; ETH/OP vectors from the reference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- decode returns (bool ok, bytes data) in every coder, AddressCoder and util.
  Internal library calls can't be try/caught, so reverting left callers no
  way to handle bad input. All custom errors are removed.
- Bech32.encode/encodeSegwit and CashAddr.encode return (bool, string);
  Bech32.encodedLength is gone. Blake2b.hash512 returns ok (tryHash512 removed).
- testFuzz_decodeNeverReverts{,_mutated} cover every coinType.
- EvmCoder: COIN_TYPE -> COIN_TYPE_ETH, EVM_BIT -> COIN_TYPE_DEFAULT,
  ENSIP-11 -> ENSIP-19, and empty bytes encode as the zero address.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adraffy adraffy changed the title Non-reverting encode, AddressCoder dispatcher, EvmCoder Non-reverting encode/decode, AddressCoder dispatcher, EvmCoder Nov 28, 2026
- encode(bytes, bool checksum) / encode(address, bool checksum): EIP-55 or
  lowercase. encode(bytes) keeps checksum = true.
- decode(string, bool checksum): with checksum, mixed-case input must be a
  valid EIP-55 checksum; without it, any case is accepted. decode(string)
  keeps checksum = true (matches the reference).
- Empty bytes no longer encode as the zero address.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adraffy
adraffy merged commit 8dfd40f into main Nov 28, 2026
1 check passed
@adraffy
adraffy deleted the encode-bool-address-coder branch November 28, 2026 01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant